package com.sharemarking.application.common.utils;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import net.sf.json.JSONObject;

/**
 * Created by Administrator on 2017/9/8.
 */
@Component
public class RefreshAccessToken {
    @Value("${wx.appid}")
    private String appid;

    @Value("${wx.secret}")
    private String secret;

    private final Logger logger = Logger.getRootLogger();

    /**
     * 定时获取access_token
     *
     */
    @Scheduled(fixedDelay=7180000)
    public String getAccessToken() throws SQLException {
        logger.info("==============开始获取access_token===============");
        String accessToken = null;
        String grantType = "client_credential";
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type="+grantType+"&appid="+appid+"&secret="+secret;
        try {
            URL urlGet = new URL(url);
            HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
            http.setRequestMethod("GET"); // 必须是get方式请求
            http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            http.connect();
            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] jsonBytes = new byte[size];
            is.read(jsonBytes);
            String message = new String(jsonBytes, "UTF-8");
            JSONObject demoJson = JSONObject.fromObject(message);
            accessToken = demoJson.getString("access_token");
            is.close();
            logger.info("==============结束获取access_token===============");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return accessToken;
    }

    /**
     * 定时获取jsapi_ticket
     *
     */
    @Scheduled(fixedDelay=7180000)
    public String getJsapiTicket() throws SQLException {
        logger.info("==============开始获取jsapi_ticket===============");
        String jsapiTicket = null;
        String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+getAccessToken()+"&type=jsapi";
        try {
            URL urlGet = new URL(url);
            HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
            http.setRequestMethod("GET"); // 必须是get方式请求
            http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            http.connect();
            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] jsonBytes = new byte[size];
            is.read(jsonBytes);
            String message = new String(jsonBytes, "UTF-8");
            JSONObject demoJson = JSONObject.fromObject(message);
            jsapiTicket = demoJson.getString("ticket");
            is.close();
            logger.info("==============结束获取jsapi_ticket===============");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsapiTicket;
    }
}
